package com.xiaoshangkou.hellopra.controller.frontdesk;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoshangkou.hellopra.bean.WangEditorResult;
import com.xiaoshangkou.hellopra.pojo.Member;
import com.xiaoshangkou.hellopra.pojo.Post;
import com.xiaoshangkou.hellopra.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/frontdesk/post")
public class FrontdeskPostController {
    @Autowired
    private PostService postService;
    /**
     * 用途：进入帖子页面时分页查询所有帖子
     * @RequestParam :设置默认值
     * page:当前页的页码（current）
     * size:每页显示的条数（size）
     */
    @RequestMapping("/routeList")
    public ModelAndView all(Integer cid,@RequestParam(defaultValue = "1")int page,
                            @RequestParam(defaultValue = "6")int size){
        ModelAndView modelAndView=new ModelAndView();
        Page<Post> postPage = postService.findLifePost(cid,page, size);
        List<Post> pageAll = postService.findAllLife(cid);
        postPage.setRecords(pageAll);
        modelAndView.addObject("postPage",postPage);
        modelAndView.setViewName("/frontdesk/post");
        return  modelAndView;
    }

    //发表帖子
    @RequestMapping("/add")
    public String add(Post post, HttpSession session){
        Member member = (Member) session.getAttribute("member");
        postService.add(post);
        return "redirect:/frontdesk/member/personalPage?mid="+member.getMid();
    }

    //帖子详情
    @RequestMapping("/postDesc")
    public ModelAndView postDesc(Integer pid){
        Post post = postService.findByPid(pid);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("post",post);
        modelAndView.setViewName("/frontdesk/post_desc");
        return modelAndView;
    }

    /*修改信息*/
    //上传图片
    @RequestMapping("/upload")
    @ResponseBody//不会跳转页面,返回字符串（json数据）
    public WangEditorResult upload(HttpServletRequest request, MultipartFile file) throws IOException {

        //创建文件夹，存放上传文件
        //拿到上传文件名
        String filename = file.getOriginalFilename();
        filename= UUID.randomUUID()+filename;
        //1设置上传文件夹的真实路径
        //配置图片上传路径，注意此处的路径和MyMvcConfig.java中配置的路径保持一致
        File uploadPathFile = new File("src/main/resources/static/"+ "/upload/img/post/");

        // 构建真实的文件路径
        File dir = new File(uploadPathFile.getAbsolutePath() + File.separator + filename);
        if (!dir.exists()){
            dir.mkdirs();
        }
        //创建空文件
//        File newFile = new File(dir, filename);
        //将上传的文件写到空文件夹中
        file.transferTo(dir);

        //构造返回结果
        WangEditorResult wangEditorResult = new WangEditorResult();
        wangEditorResult.setErrno(0);
        String[] data={"/upload/img/post/"+filename};
        wangEditorResult.setData(data);

        return  wangEditorResult;

    }
}
